package top.shiyiri.servlet;

import com.google.gson.Gson;
import top.shiyiri.pojo.Person;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Aunean
 * @date 2022/2/12 20:04
 */
public class AjaxServlet extends BaseServlet {

    protected void javaScriptAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");//解决响应中文乱码
        System.out.println("Ajax请求过来了");

        Person person = new Person(1, "颤三");

        Gson gson = new Gson();
        String toJson = gson.toJson(person);

        resp.getWriter().write(toJson);
    }

    protected void jQueryAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");//解决响应中文乱码
        System.out.println("jQueryAjax请求过来了");

        Person person = new Person(1, "颤三");

        Gson gson = new Gson();
        String toJson = gson.toJson(person);

        resp.getWriter().write(toJson);
    }

    protected void JQueryGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");//解决响应中文乱码
        System.out.println("JQueryGet请求过来了");

        Person person = new Person(1, "颤三");

        Gson gson = new Gson();
        String toJson = gson.toJson(person);

        resp.getWriter().write(toJson);
    }

    protected void JQueryPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");//解决响应中文乱码
        System.out.println("JQueryPost请求过来了");

        Person person = new Person(1, "颤三");

        Gson gson = new Gson();
        String toJson = gson.toJson(person);

        resp.getWriter().write(toJson);
    }

    protected void JQueryGetJSON(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");//解决响应中文乱码
        System.out.println("JQueryGetJSON请求过来了");

        Person person = new Person(1, "颤三");

        Gson gson = new Gson();
        String toJson = gson.toJson(person);

        resp.getWriter().write(toJson);
    }
    protected void JQuerySerialize(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");//解决响应中文乱码
        System.out.println("JQuerySerialize请求过来了");

        System.out.println("用户名：" + req.getParameter("username"));
        System.out.println("密码：" + req.getParameter("password"));

        Person person = new Person(1, "颤三");

        Gson gson = new Gson();
        String toJson = gson.toJson(person);
        resp.getWriter().write(toJson);
    }
}
